package com.bestcem.xm.om.dao.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.ObjectId;
import com.alibaba.fastjson.JSON;
import com.bestcem.xm.om.dao.EmailTemplateDao;
import com.bestcem.xm.om.dao.mapper.EmailTemplateMapper;
import com.bestcem.xm.om.entity.EmailTemplate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * 描述信息
 *
 * @author xuan.yang <xuan.yang@idiaoyan.com>
 * @version v1.0
 * @date 2021/11/15 18:14
 */
@Repository
public class EmailTemplateDaoImpl implements EmailTemplateDao {
    @Resource
    private EmailTemplateMapper emailTemplateMapper;

    @Override
    public EmailTemplate getMailTemplateById(String id) {
        return emailTemplateMapper.getMailTemplateById(id);
    }

    @Override
    public String saveMailTemplate(EmailTemplate emailTemplate) {
        if (!ObjectId.isValid(emailTemplate.getId())) {
            emailTemplate.setId(ObjectId.next());
        }
        emailTemplateMapper.saveMailTemplate(emailTemplate);
        return emailTemplate.getId();
    }

    @Override
    public Long updateFristById(String templateId, String topic, String content, List<String> linkList) {
        if (!ObjectId.isValid(templateId) || StringUtils.isBlank(content)) {
            return 0L;
        }
        String linkString = null;
        if (CollUtil.isNotEmpty(linkList)) {
            linkString = JSON.toJSONString(linkList);
        }
        return emailTemplateMapper.updateFristById(templateId, topic, content, linkString);
    }

    @Override
    public List<EmailTemplate> listByIds(List<String> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return Collections.emptyList();
        }
        return emailTemplateMapper.listByIds(idList);
    }
}
